package net.googlecode.exigenlab.task5;

import net.googlecode.exigenlab.task5.model.Manager;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by IntelliJ IDEA.
 * User: Loke
 * Date: 12.04.11
 * Time: 22:18
 * To change this template use File | Settings | File Templates.
 */
public class Task5 {

    /**
     * Class-comparator to compare managers to Last name.
     */
    private class SalaryComparator implements Comparator<Manager> {
        @Override
        public int compare(Manager object1, Manager object2) {
            return object1.getLastName().compareTo(object2.getLastName());
        }
    }

    /**
     * Sorts input ArrayList to personal code, delete persons with minimum
     * and maximum salary and prints report.
     *
     * @param list Input ArrayList
     */
    public void codeSort(final ArrayList<Manager> list) {
        Collections.sort(list, new SalaryComparator());

        list.remove(0);
        list.remove(list.size() - 1);
        Collections.sort(list, new Comparator<Manager>() {
            @Override
            public int compare(Manager object1, Manager object2) {
                return (object1.getPersonalId() < object2.getPersonalId() ? -1
                        : (object1.getPersonalId() == object2.getPersonalId() ? 0 : 1));
            }
        });
        for (Manager manager : list) {
            System.out.println(manager);
        }
    }


    /**
     * Sorts input ArrayList to Last name and prints report.
     *
     * @param list Input ArrayList
     */
    public void nameSort(final ArrayList<Manager> list) {

        Collections.sort(list, new Comparator<Manager>() {
            @Override
            public int compare(Manager object1, Manager object2) {

                return object1.getLastName().compareTo(object2.getLastName());
            }
        });

        for (Manager manager : list) {
            if (manager.getAge() < 45) {
                System.out.println(manager);
            }
        }
    }

    /**
     * Sorts input ArrayList to personal salary and prints report.
     *
     * @param list Input ArrayList
     */

    public void salarySort(final ArrayList<Manager> list) {
        Collections.sort(list, new SalaryComparator());

        BigDecimal prevsalary = new BigDecimal(-1);

        for (Manager x : list) {
            if (x.getSalary().compareTo(prevsalary) != 0) {
                System.out.println(x);
                prevsalary = x.getSalary();
            }
        }
    }
}
